/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.startup.musicstore.services.Impl;

import com.startup.musicstore.model.Customer;
import com.startup.musicstore.model.OrderItem;
import com.startup.musicstore.model.Orders;
import com.startup.musicstore.services.OrderAmountService;
import com.startup.musicstore.services.crud.CustomerCrudService;
import com.startup.musicstore.services.crud.CustomerInvoiceCrudService;
import com.startup.musicstore.services.crud.OrderItemCrudService;
import com.startup.musicstore.services.crud.OrdersCrudService;
import java.math.BigDecimal;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author linda
 */
@Service("orderAmountService")
public class OrderAmountServiceImpl implements OrderAmountService{
    
    @Autowired
    private CustomerCrudService customerCrudService;
    
    @Autowired
    private CustomerInvoiceCrudService customerInvoiceCrudService;
    
  

    @Override
    public void calculateOrderAmount() {
        
        List<Customer> customers = customerCrudService.findAll();
        BigDecimal quantity;
        BigDecimal orderAmount = new BigDecimal(0);
        BigDecimal unitPrice;
        
        for(Customer cust : customers)
        {
            List<Orders> orders = cust.getOrders();
            for(Orders ord : orders)
            {
                List<OrderItem> items = ord.getItems();
                for(OrderItem its : items)
                {
                    quantity = new BigDecimal(its.getQuantity());
                    unitPrice = its.getAlbum().getUnitPrice();
                    orderAmount = orderAmount.add(unitPrice.multiply(quantity));
                }

                ord.getCustomerInvoice().setOrderAmount(orderAmount);
                customerInvoiceCrudService.merge(ord.getCustomerInvoice());
                orderAmount = new BigDecimal(0);

            }
        }
    }
    
    
}
